<template>
  <div class="dashboard-container">
    <h2 class="head-big-title">
      Laporan Jualan Produk
    </h2>
    <el-date-picker
      style="width: 92%;margin: 0 4%;"
      v-model="dateTime"
      type="daterange"
      @change="getResetLoadData"
      align="right"
      unlink-panels
      format="yyyy-MM-dd"
      value-format="yyyy-MM-dd"
      range-separator="To"
      start-placeholder="start"
      end-placeholder="end">
    </el-date-picker>
    <h2 style="text-align: center;line-height: 36px">Jumlah Jualan: {{$formatMoneyStr(totalSales)}}</h2>
    <el-form label-position="top" label-width="80px" :model="searchData" style="width: 92%;margin: 20px 4% 10px 4%;padding: 6px 2%;border-radius: 10px;background-color: white;text-align: left;clear: both;">
      <el-form-item label="Nama Produk" style="display: block;">
        <el-input v-model="searchData.productName" @change="getResetLoadData" style="width: 100%;"></el-input>
      </el-form-item>
    </el-form>
    <div style="width: 96%;margin: 20px 2% 0 2%;padding-left: 10px;border-radius: 20px;background-color: rgb(245,246,247);text-align: left;">
      <div v-for="(item,index) in tableData" :key="index" style="width: 100%;border-radius: 10px;background-color: white;padding: 6px 5px 15px 6px;margin-bottom: 10px;border:grey 1px solid;">
        <div style="width: 100%;">
          <strong>Nama Produk</strong>
          <p>{{item.productName}}</p>
        </div>
        <div style="width: 100%;">
          <div style="width: 50%;display: inline-block;line-height: 27px;">
            <strong>Kuantiti</strong>
            <p>{{item.totalQty}}</p>
          </div>
          <div style="width: 49%;display: inline-block;line-height: 27px;">
            <strong>Jumlah Pesanan</strong>
            <p>{{item.totalPOs}}</p>
          </div>
        </div>
        <div style="width: 100%;height: 60px;">
          <div style="width: 50%;display: inline-block;text-align: left;vertical-align: top;">
            <strong style="line-height: 28px;">Jumlah</strong>
            <p>{{ item.totalAmt?$formatMoneyStr(item.totalAmt):'RM 0.00'}}</p>
          </div>
          <div style="width: 49%;display: inline-block;text-align: left;">
            <strong style="line-height: 28px;">Keuntungan</strong>
            <p>
              {{ item.totalProfit?$formatMoneyStr(item.totalProfit):'RM 0.00'}}
              ({{$keepTwoDecimalFull((item.totalProfit /item.totalAmt) * 100)}}%)
            </p>
          </div>
        </div>
      </div>
    </div>
    <el-pagination
      style="float: right;margin: 16px 4% 10px"
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :page-size="searchData.pageNum"
      :current-page="searchData.pageIndex"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { salesProductReportApi } from '@/api/user'

export default {
  name: 'Customers',
  computed: {
    ...mapGetters([
      'role'
    ])
  },
  data() {
    return {
      loading: false,
      showActionButton: true,
      tableData: [],
      dateTime: [],
      searchData: {
        pageNum: 10,
        pageIndex: 1
      },
      total: 0,
      totalSales: 0
    }
  },
  created() {
    this.checkResetTime()
    this.loadData()
  },
  mounted() {
  },
  methods: {
    checkResetTime() {
      const now = new Date()
      let monthStr = now.getMonth() + 1
      let startTime = ''
      let endTime = ''
      endTime = now.getFullYear() + ((monthStr < 10) ? '-0' : '-') + monthStr + '-' + now.getDate()
      startTime = ''
      if (now.getDate() < 10) { // 10日前
        if (monthStr == 1) {
          startTime = (now.getFullYear() - 1) + '-12' + '-' + now.getDate()
        } else {
          monthStr = monthStr - 1
          startTime = now.getFullYear() + ((monthStr < 10) ? '-0' : '-') + monthStr + '-' + now.getDate()
        }
      } else {
        startTime = now.getFullYear() + ((monthStr < 10) ? '-0' : '-') + monthStr + '-1'
      }
      this.dateTime = [startTime, endTime]
    },
    getResetLoadData() {
      this.searchData.pageIndex = 1
      this.loadData()
    },
    showDetail(row) {
      this.showActionButton = false
      this.searchData.dealerId = row.dealerId
      this.loadData()
    },
    loadData() {
      let requestData = this.searchData
      requestData.startTime = this.dateTime[0]
      requestData.endTime = this.dateTime[1]
      salesProductReportApi(requestData).then(res => {
        this.tableData = res.data.tradDetail.records
        this.total = res.data.tradDetail.total
        this.totalSales = res.data.totalSales
      }).catch(err => {
        console.log(err)
      })
    },
    handleCurrentChange(val) {
      this.searchData.pageIndex = val
      this.loadData()
    }
  }
}
</script>

<style lang="scss" scoped>
  .head-big-title{
    text-align: left;
    font-size: 20px;
    margin: 20px 0 20px 3%;
  }
  .dashboard-container{
    margin-top: 20px;
    text-align: center;
  }

</style>
<style scoped>
  /deep/.head-search label{
    line-height: 30px;
  }
</style>
